package com.deshan.websys.api.utils;


import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CachUtil {
  /* 设置缓存对象
  * @param cacheManager
  * @param key
  * @param object
  */

    private CacheManager cacheManager = new CacheManager(CachUtil.class.getClassLoader().getResourceAsStream("ehcache.xml"));;

    public  void setCache(String key, Object object){
        Cache cache = cacheManager.getCache("mobileCache");
        Element element = new Element(key,object);
        cache.put(element);
    }
    /**
     * 从缓存中取出对象
     * @param key
     * @return
     */
    public Object getCache(String key){
        Object object = null;
        Cache cache = cacheManager.getCache("mobileCache");
        if(cache.get(key)!=null && !cache.get(key).equals("")){
            object = cache.get(key).getObjectValue();
        }
        return object;
    }
    public  List getKeys(){
        Cache cache = cacheManager.getCache("mobileCache");
        return cache.getKeys();
    }
    public  void removeCache(String key)
    {
        try
        {
            Cache c = cacheManager.getCache("mobileCache");
            if (c != null) {
                c.remove(key);
            }
        } catch (Exception e) {
        }
    }
}
